package com.jiulong.service.Impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiulong.dao.TProductMapper;
import com.jiulong.model.TProduct;
import com.jiulong.model.TProductExample;
import com.jiulong.model.TProductExample.Criteria;
import com.jiulong.service.ProductService;
@Service
public class ProductServiceImpl implements ProductService{
	@Autowired
	TProductMapper tProductMapper;
	@Override
	public List findProductByDefault(Map params){
		TProductExample e=new TProductExample();e.createCriteria().andIstimepromotionEqualTo("y");
		List<TProduct> list=tProductMapper.selectByExample(e);
		
		return sortProduct(list);
		
	}
	
	
	public List findProductBysele(Map params) {
		// TODO Auto-generated method stub
		TProductExample tProductExample=new TProductExample();
		
		
		Criteria c=tProductExample.createCriteria();
		c.andAddressLike((String)params.get("city"));
		if(params.get("breakfirst").equals(1)){
			//tProductExample.createCriteria().andZaocanEqualTo(1);
			c.andZaocanEqualTo(1);
		}
		if(params.get("meettingRoom").equals(1)){
			c.andMeettingEqualTo(1);
		}
		if(params.get("wifi").equals(1)){
			c.andWifiEqualTo(1);
		}
		if(params.get("park").equals(1)){
			c.andCarstateEqualTo(1);
		}
		if(params.get("visitor").equals(1)){
			c.andVisitorEqualTo(1);
		}
		
		List<TProduct> list=tProductMapper.selectByExample(tProductExample);
		if(list.size()==0){
			tProductExample.clear();
			
			Criteria cc=tProductExample.createCriteria();
			cc.andAddressLike((String)params.get("city1"));
			if(params.get("breakfirst").equals(1)){
				cc.andZaocanEqualTo(1);
			}
			if(params.get("meettingRoom").equals(1)){
				cc.andMeettingEqualTo(1);
			}
			if(params.get("wifi").equals(1)){
				cc.andWifiEqualTo(1);
			}
			if(params.get("park").equals(1)){
				cc.andCarstateEqualTo(1);
			}
			if(params.get("visitor").equals(1)){
				cc.andVisitorEqualTo(1);
			}
			list=tProductMapper.selectByExample(tProductExample);
		}
		
		
		
		
		return sortProduct(list);
	}
	public List<TProduct> sortProduct(List<TProduct> list){
		if(list.size()!=0){
			for(int i=0;i<list.size();i++){
				if(i==0&&list.get(i).prodList.size()==0||list.size()==1){
					list.get(i).prodList.add(list.get(i));
				}
				if(list.size()!=(i+1)){
				if(list.get(i).getName().equals(list.get(i+1).getName())){
				list.get(i).prodList.add(list.get(i+1));
				list.remove(i+1);
				i--;}else{
					list.get(i+1).prodList.add(list.get(i+1));
				}}
				
				
			}
			for(TProduct t:list){
				sortStringMethod(t.prodList);
			}
			
			
		}
		return list;
		
	}
	
	
	public void sortStringMethod(List list){  
	    Collections.sort(list, new Comparator(){  
	        @Override  
	        public int compare(Object o1, Object o2) {  
	        	TProduct t1=(TProduct)o1;  
	        	TProduct t2=(TProduct)o2;  
	            return t1.getTitle().compareTo(t2.getTitle());  
	        }             
	    });  
	}

}
